<?php

	//get the variables	
	$fileName = $_POST['fileName'];
	$class = $_POST['class'];
	$professor = $_POST['professor'];
	$file = $_POST['file'];
	$privacy = $_POST['privacy'];
	$userId = $_POST['userId'];
	
	//get the ids
	$classID = addClass($class);
	$professorID = addProfessor($professor);
	$privacyID = getPrivacy($privacy);
	
	//make the query
	$query = "INSERT into CS275.files (name, file, userId, course, professor, privacy) VALUES ('$fileName', '$file', '$userId', '$classID', '$professorID', '$privacyID')";	
	$successful = true;
	
	
	
	//connect to database
	$link = mysql_connect('localhost', 'root', 'admin');
	if(!$link){
		$successful = false;
		die('Could not connect: ' . mysql_error());	
	}
	
	//perform query
	$result = mysql_query($query, $link);
	
	//if it failed
	if (!$result) {
		$successful = false;
    	$message  = 'Invalid query: ' . mysql_error() . "\n";
    	$message .= 'Whole query: ' . $query;
    	die($message);
	}
	
	//free results
	mysql_free_result($result);
	
	//close link
	mysql_close($link);
	
	//make false print false, true for true
	$boolarray = Array(false => 'false', true => 'true');
	
	//output json
	echo(json_encode(array("successful" => $boolarray[$successful])));
	
	
	function getPrivacy($privacy){
		$query = "SELECT * FROM CS275.privacy WHERE privacy='$privacy'";
		
		//establish connection to MySQL
		$link = mysql_connect('localhost', 'root', 'admin');
		if(!$link){
			$successful = false;
			die('Could not connect: ' . mysql_error());	
		}
		
		//perform query
		$result = mysql_query($query, $link);
		
		$id = 1;
		//if we get results
		while($row = mysql_fetch_assoc($result)){
			$id = $row["id"];
		}
		
		//free results
		mysql_free_result($result);
		
		//close link
		mysql_close($link);
		
		//output json
		return $id;
	}
	
	function checkProfessor($prof){
		$query = "SELECT * FROM CS275.professor WHERE professor='$prof'";
		
		//establish connection to MySQL
		$link = mysql_connect('localhost', 'root', 'admin');
		if(!$link){
			$successful = false;
			die('Could not connect: ' . mysql_error());	
		}
		
		//perform query
		$result = mysql_query($query, $link);
		
		$id = -1;
		//if we get results
		while($row = mysql_fetch_assoc($result)){
			$id = $row["id"];
		}
		
		//free results
		mysql_free_result($result);
		
		//close link
		mysql_close($link);
		
		//output json
		return $id;
	}
	
	function addProfessor($prof){
		
		$result = checkProfessor($prof);
		
		if($result == -1){
			$query = "INSERT into CS275.professor (professor) VALUES ('$prof')";
		
			//establish connection to MySQL
			$link = mysql_connect('localhost', 'root', 'admin');
			if(!$link){
				$successful = false;
				die('Could not connect: ' . mysql_error());	
			}
			
			//do the first query
			$result = mysql_query($query, $link);
			
			//if it failed
			if (!$result) {
				$successful = false;
				$message  = 'Invalid query: ' . mysql_error() . "\n";
				$message .= 'Whole query: ' . $query;
				die($message);
			}
			
			//get the row id
			$id = mysql_insert_id();	
			
			//free the result
			mysql_free_result($result);	
			
			return $id;
		}else{
			return $result;	
		}
		
		
	}
	
	function checkClass($class){
		$query = "SELECT * FROM CS275.class WHERE course='$class'";
		
		//establish connection to MySQL
		$link = mysql_connect('localhost', 'root', 'admin');
		if(!$link){
			$successful = false;
			die('Could not connect: ' . mysql_error());	
		}
		
		//perform query
		$result = mysql_query($query, $link);
		
		$id = -1;
		//if we get results
		while($row = mysql_fetch_assoc($result)){
			$id = $row["id"];
		}
		
		//free results
		mysql_free_result($result);
		
		//close link
		mysql_close($link);
		
		//output json
		return $id;
	}
	
	
	function addClass($class){
		
		$result = checkClass($class);
		
		if($result == -1){
			$query = "INSERT into CS275.class (course) VALUES ('$class')";
		
			//establish connection to MySQL
			$link = mysql_connect('localhost', 'root', 'admin');
			if(!$link){
				$successful = false;
				die('Could not connect: ' . mysql_error());	
			}
			
			//do the first query
			$result = mysql_query($query, $link);
			
			//if it failed
			if (!$result) {
				$successful = false;
				$message  = 'Invalid query: ' . mysql_error() . "\n";
				$message .= 'Whole query: ' . $query;
				die($message);
			}
			
			//get the row id
			$id = mysql_insert_id();	
			
			//free the result
			mysql_free_result($result);	
			
			return $id;
		}else{
			return $result;	
		}		
		
	}


?>